package com.ruoyi.jzzgl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.jzzgl.domain.entity.JzzglZdgl;
import com.ruoyi.jzzgl.domain.bo.JzzglZdglBo;
import com.ruoyi.jzzgl.domain.vo.JzzglZdglVo;
import com.ruoyi.jzzgl.mapper.JzzglZdglMapper;
import com.ruoyi.jzzgl.service.JzzglZdglService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 站点管理Service业务层处理
 *
 * @author xq
 */
@RequiredArgsConstructor
@Service
public class JzzglZdglServiceImpl extends ServiceImpl<JzzglZdglMapper, JzzglZdgl>
    implements JzzglZdglService {

    @Resource
    JzzglZdglMapper jzzglZdglMapper;

    /**
     * 分页查询站点列表
     */
    @Override
    public TableDataInfo<JzzglZdglVo> queryPageList(JzzglZdglBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<JzzglZdgl> lqw = Wrappers.lambdaQuery();

        lqw.like(StringUtils.isNotBlank(bo.getStationName()), JzzglZdgl::getStationName, bo.getStationName())
           .like(StringUtils.isNotBlank(bo.getStationAddress()), JzzglZdgl::getStationAddress, bo.getStationAddress())
           .like(StringUtils.isNotBlank(bo.getContactPerson()), JzzglZdgl::getContactPerson, bo.getContactPerson())
           .like(StringUtils.isNotBlank(bo.getContactPhone()), JzzglZdgl::getContactPhone, bo.getContactPhone());

        Page<JzzglZdglVo> result = jzzglZdglMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询站点列表
     */
    @Override
    public List<JzzglZdglVo> selectList(JzzglZdglBo bo) {
        LambdaQueryWrapper<JzzglZdgl> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getStationName()), JzzglZdgl::getStationName, bo.getStationName())
            .like(StringUtils.isNotBlank(bo.getStationAddress()), JzzglZdgl::getStationAddress, bo.getStationAddress())
            .like(StringUtils.isNotBlank(bo.getContactPerson()), JzzglZdgl::getContactPerson, bo.getContactPerson())
            .like(StringUtils.isNotBlank(bo.getContactPhone()), JzzglZdgl::getContactPhone, bo.getContactPhone());

        List<JzzglZdgl> list = this.list(lqw);
        return BeanUtil.copyToList(list, JzzglZdglVo.class);
    }

    /**
     * 新增站点
     */
    @Override
    public void insertByBo(JzzglZdglBo bo) {
        JzzglZdgl add = new JzzglZdgl();
        BeanUtil.copyProperties(bo, add, "id");  // 忽略 id 字段
        save(add);
    }

    /**
     * 修改站点
     */
    @Override
    public void updateByBo(JzzglZdglBo bo) {
        JzzglZdgl update = BeanUtil.toBean(bo, JzzglZdgl.class);
        updateById(update);
    }

    /**
     * 批量删除站点
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByIds(Long[] ids) {
        removeByIds(Arrays.asList(ids));
    }
}




